package com.jwgf.mqtt.mqttactions;

import android.content.Intent;
import android.net.Uri;

import com.alibaba.fastjson.JSONObject;
import com.jwgf.common.AppContext;
import com.jwgf.common.Utility;
import com.jwgf.mqtt.MqttAction;

import java.io.File;

public class UpgradeAction implements MqttAction {

    @Override
    public void action(JSONObject object) {
        String fileName = object.getString("file_name");
        long fileLength = object.getLong("file_size");
        String fileData = object.getString("value");

        File dir = AppContext.getInstance().getFilesDir();
        File file = new File(dir, "U_" + fileName);
//        Utility.FileUtils.encodedString2File(fileData, file);
//        installApk(file);
    }

    public void installApk(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()),
                "application/vnd.android.package-archive");
        AppContext.getInstance().startActivity(intent);
    }

}
